package com.wuhunyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuhunyu.client.VideoServiceClient;
import com.wuhunyu.commonutils.check.Check;
import com.wuhunyu.commonutils.result.CommonResult;
import com.wuhunyu.entities.EduVideo;
import com.wuhunyu.mapper.EduChapterMapper;
import com.wuhunyu.mapper.EduCourseMapper;
import com.wuhunyu.mapper.EduVideoMapper;
import com.wuhunyu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuhunyu.servicebase.exception.Assert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author wuhunyu
 * @since 2021-01-17
 */
@Service("eduVideoService")
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Resource
    private EduChapterMapper eduChapterMapper;

    @Resource
    private EduCourseMapper eduCourseMapper;

    @Resource
    private EduVideoMapper eduVideoMapper;

    @Resource
    private VideoServiceClient videoServiceClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult addVideo(EduVideo eduVideo) {
        // 参数校验
        // 对象非空
        Assert.isRight(Check.isBlank(eduVideo), "小节对象不能为空");
        // 小节id为空
        Assert.isRight(Check.isNotBlank(eduVideo.getId()), "小节id必须为空");
        // 课程id非空
        Assert.isRight(Check.isBlank(eduVideo.getCourseId()), "课程id不能为空");
        // 章节id非空
        Assert.isRight(Check.isBlank(eduVideo.getChapterId()), "章节id不能为空");
        // 小节标题不能为空
        Assert.isRight(Check.isBlank(eduVideo.getTitle()), "小节标题不能为空");
        // 小节排序
        Assert.isRight(eduVideo.getSort() == null || eduVideo.getSort() < 0, "小节排序不合法");
        // 视频免费
        Assert.isRight(!(eduVideo.getIsFree() == 0 || eduVideo.getIsFree() == 1), "视频免费标志位非法");
        // 视频id
        System.out.println(eduVideo);
        Assert.isRight(Check.isBlank(eduVideo.getVideoSourceId()), "视频id不能为空");
        // 视频名称
        Assert.isRight(Check.isBlank(eduVideo.getVideoOriginalName()), "视频名称不能为空");
        // 视频大小
        Assert.isRight(Check.isBlank(eduVideo.getSize()), "视频大小不能为空");
        // 视频状态
        Assert.isRight(Check.isBlank(eduVideo.getStatus()), "视频状态不能为空");
        // 校验课程是否存在
        Assert.isRight(Check.isBlank(eduCourseMapper.selectById(eduVideo.getCourseId())), "课程不存在");
        // 章节是否存在
        Assert.isRight(Check.isBlank(eduChapterMapper.selectById(eduVideo.getChapterId())), "章节不存在");
        // 小节标题是否已存在
        QueryWrapper<EduVideo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id", eduVideo.getCourseId());
        queryWrapper1.eq("chapter_id", eduVideo.getChapterId());
        queryWrapper1.eq("title", eduVideo.getTitle());
        Assert.isRight(eduVideoMapper.selectCount(queryWrapper1) > 0, "小节标题已存在");
        // 排序是否已存在
        QueryWrapper<EduVideo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("course_id", eduVideo.getCourseId());
        queryWrapper2.eq("chapter_id", eduVideo.getChapterId());
        queryWrapper2.eq("sort", eduVideo.getSort());
        Assert.isRight(eduVideoMapper.selectCount(queryWrapper2) > 0, "小节排序已存在");
        // 执行小节新增操作
        // 播放次数
        eduVideo.setPlayCount(0L);
        Assert.isRight(eduVideoMapper.insert(eduVideo) != 1, "新增小节信息失败");
        return CommonResult.success("新增小节信息成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult updateVideo(EduVideo eduVideo) {
        // 参数校验
        // 对象非空
        Assert.isRight(Check.isBlank(eduVideo), "小节对象不能为空");
        // 小节id非空
        Assert.isRight(Check.isBlank(eduVideo.getId()), "小节id不能为空");
        // 课程id非空
        Assert.isRight(Check.isBlank(eduVideo.getCourseId()), "课程id不能为空");
        // 章节id非空
        Assert.isRight(Check.isBlank(eduVideo.getChapterId()), "章节id不能为空");
        // 小节标题不能为空
        Assert.isRight(Check.isBlank(eduVideo.getTitle()), "小节标题不能为空");
        // 小节排序
        Assert.isRight(eduVideo.getSort() == null || eduVideo.getSort() < 0, "小节排序不合法");
        // 视频免费
        Assert.isRight(!(eduVideo.getIsFree() == 0 || eduVideo.getIsFree() == 1), "视频免费标志位非法");
        // 视频id
        Assert.isRight(Check.isBlank(eduVideo.getVideoSourceId()), "视频id不能为空");
        // 视频名称
        Assert.isRight(Check.isBlank(eduVideo.getVideoOriginalName()), "视频名称不能为空");
        // 视频大小
        Assert.isRight(Check.isBlank(eduVideo.getSize()), "视频大小不能为空");
        // 视频状态
        Assert.isRight(Check.isBlank(eduVideo.getStatus()), "视频状态不能为空");
        // 校验课程是否存在
        Assert.isRight(Check.isBlank(eduCourseMapper.selectById(eduVideo.getCourseId())), "课程不存在");
        // 章节是否存在
        Assert.isRight(Check.isBlank(eduChapterMapper.selectById(eduVideo.getChapterId())), "章节不存在");
        // 校验课程是否存在
        Assert.isRight(Check.isBlank(eduCourseMapper.selectById(eduVideo.getCourseId())), "课程不存在");
        // 章节是否存在
        Assert.isRight(Check.isBlank(eduChapterMapper.selectById(eduVideo.getChapterId())), "章节不存在");
        // 小节标题是否已存在，且不重复
        QueryWrapper<EduVideo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.ne("id", eduVideo.getId());
        queryWrapper1.eq("course_id", eduVideo.getCourseId());
        queryWrapper1.eq("chapter_id", eduVideo.getChapterId());
        queryWrapper1.eq("title", eduVideo.getTitle());
        Assert.isRight(eduVideoMapper.selectCount(queryWrapper1) > 0, "小节标题已存在");
        // 排序是否已存在，且不重复
        QueryWrapper<EduVideo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.ne("id", eduVideo.getId());
        queryWrapper2.eq("course_id", eduVideo.getCourseId());
        queryWrapper2.eq("chapter_id", eduVideo.getChapterId());
        queryWrapper2.eq("sort", eduVideo.getSort());
        Assert.isRight(eduVideoMapper.selectCount(queryWrapper2) > 0, "小节排序已存在");
        // 执行修改操作
        Assert.isRight(eduVideoMapper.updateById(eduVideo) < 0, "小节信息更新失败");
        return CommonResult.success("小节信息更新成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult deleteVideo(String id) {
        // 校验参数
        // id
        Assert.isRight(Check.isBlank(id), "id不能为空");
        // 查询小节是否存在
        EduVideo eduVideo = baseMapper.selectById(id);
        Assert.isRight(Check.isBlank(baseMapper.selectById(id)), "小节不存在");
        // 执行删除操作
        Assert.isRight(baseMapper.deleteById(id) != 1, "删除小节失败");
        // 校验视频id是否存在
        if (Check.isNotBlank(eduVideo.getVideoSourceId())) {
            // 存在则删除
            videoServiceClient.deleteVideo(eduVideo.getVideoSourceId());
        }
        return CommonResult.success("删除小节成功");
    }

}
